package com.atguigu.jxc.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐振展
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;



    /**
     * 供应商下拉列表查询
     * @param q
     * zjh
     * @return
     */
    @PostMapping("/getComboboxList")
    public String getComboboxList(String q){
        return JSON.toJSONString(supplierService.getComboboxList(q));

    }
    /**
     * 分页查询供应商
     * http://localhost:8080/supplier/list
     * @param page 当前页数
     * @param rows 每页显示的记录数
     * @param supplierName 供应商名称
     * @return
     */
    @PostMapping("list")
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        IPage<Supplier> pageParam = new Page<>(page, rows);
        if (supplierName == null) {
            supplierName = "";
        }
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("supplier_name", supplierName);
        supplierService.page(pageParam, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total", pageParam.getTotal());
        map.put("rows", pageParam.getRecords());
        return map;
    }

    /**
     * 供应商添加或修改
     * http://localhost:8080/supplier/save?supplierId=1
     * @param supplier
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(Supplier supplier) {
        boolean b = supplierService.saveOrUpdate(supplier);
        if (b == true) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(ErrorCode.REQ_ERROR_CODE, ErrorCode.REQ_ERROR_MESS);
        }
    }

    /**
     * 删除供应商（支持批量删除）
     * http://localhost:8080/supplier/delete
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(String ids) {
        String[] split = ids.split(",");
        boolean b = supplierService.removeByIds(Arrays.asList(split));
        if (b == true) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(ErrorCode.REQ_ERROR_CODE, ErrorCode.REQ_ERROR_MESS);
        }
    }


}
